set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
cmake_minimum_required(VERSION 3.26)

set(CROSS_COMPILE "riscv-none-elf-")
set(CMAKE_MAKE_PROGRAM "C:/MounRiver/MounRiver_Studio/toolchain/Build Tools/bin/make")
set(OPENOCD_PATH "C:/MounRiver/MounRiver_Studio/toolchain/OpenOCD/bin")

# specify cross-compilers and tools
set(CMAKE_C_COMPILER ${CROSS_COMPILE}gcc)
set(CMAKE_CXX_COMPILER ${CROSS_COMPILE}g++)
set(CMAKE_ASM_COMPILER ${CROSS_COMPILE}gcc)
set(CMAKE_AR ${CROSS_COMPILE}ar)
set(CMAKE_OBJCOPY ${CROSS_COMPILE}objcopy)
set(CMAKE_OBJDUMP ${CROSS_COMPILE}objdump)
set(SIZE ${CROSS_COMPILE}size)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

project(CH32X033_DAP C ASM)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)

set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/Ld/Link.ld)

include_directories(
        "Core"
        "Debug"
        "Peripheral/inc"
        "User"
        "CherryUSB/common"
        "CherryUSB/core"
        "CherryUSB/class/cdc"
        "CherryUSB/class/msc"
        "CherryUSB/class/hid"
        "CherryRB"
        "CherryDAP"
        "CherryDAP/DAP/Include"
        "crc"
        "SwdHost"
)

file(GLOB_RECURSE SOURCES
        "Core/*.c"
        "Debug/*.c"
        "Peripheral/src/*.c"
        "Startup/startup_ch32x035.S"
        "User/*.c"
        "CherryUSB/core/usbd_core.c"
        "CherryUSB/port/ch32/usb_ch58x_dc_usbfs.c"
        "CherryUSB/class/cdc/usbd_cdc.c"
        "CherryUSB/class/hid/usbd_hid.c"
        "CherryRB/*.c"
        "CherryDAP/dap_descriptors.c"
        "CherryDAP/dap_main.c"
        "CherryDAP/DAP/Source/DAP.c"
        "CherryDAP/DAP/Source/DAP_vendor.c"
        "CherryDAP/DAP/Source/JTAG_DP.c"
        "CherryDAP/DAP/Source/SW_DP.c"
        "crc/*.c"
        "SwdHost/*.c"
)

set(CMAKE_C_FLAGS_DEBUG "-O3 -g")
set(CMAKE_C_FLAGS_RELEASE "-O3 -s")

add_compile_options(
        -march=rv32imacxw -mabi=ilp32 -msmall-data-limit=8 -msave-restore -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common -Wunused -Wuninitialized
)

add_link_options(
        -T ${LINKER_SCRIPT} -nostartfiles -Xlinker --gc-sections -Wl,--print-memory-usage -Wl,-Map,${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map --specs=nano.specs --specs=nosys.specs
)
add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT})

set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)
set(LST_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.lst)

add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
        COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
        COMMAND ${CMAKE_OBJDUMP} --all-headers --demangle --disassemble $<TARGET_FILE:${PROJECT_NAME}.elf> > ${LST_FILE}
        COMMAND ${SIZE} --format=berkeley $<TARGET_FILE:${PROJECT_NAME}.elf>
)

# -c "flash erase_sector wch_riscv 0 last"
add_custom_target(
    DOWNLOAD
    COMMAND "${OPENOCD_PATH}/openocd.exe" -f "${OPENOCD_PATH}/wch-riscv.cfg" -c init -c halt -c "program $<TARGET_FILE:${PROJECT_NAME}.elf>" -c "verify_image $<TARGET_FILE:${PROJECT_NAME}.elf>" -c wlink_reset_resume -c exit
)